import tkinter as tk
from model import *
from tkinter import messagebox
from tkinter.ttk import Notebook, Treeview, Scrollbar

root = tk.Tk()
root.geometry('500x400')
root.title('学生信息管理系统')
current_user = notebook = Notebook()


class LoginFrame(tk.Frame):
    def __init__(self):
        super().__init__(master=root)
        
        self.teacher_name_var = tk.StringVar()
        self.teacher_password_var = tk.StringVar()
        

    def create_window(self):
        tk.Label(self, text='教师名:').grid(row=0, column=0, pady=5)
        tk.Entry(self, textvariable=self.teacher_name_var).grid(row=0, column=1, pady=5)
        tk.Label(self, text='密码').grid(row=1, column=0)
        tk.Entry(self, show='*', textvariable=self.teacher_password_var).grid(row=1, column=1, pady=5)
        tk.Button(self, text='登录', command=self.login).grid(row=2, column=0, ipadx=5, pady=7)
        tk.Button(self, text='注册', command=self.register).grid(row=2, column=1, ipadx=5, pady=5)
        

    def login(self):
        
        global current_user
        teacher_data = session.query(Teacher).filter_by(name=self.teacher_name_var.get(),
                                                        password=self.teacher_password_var.get()).first()
        messagebox.showinfo('学生信息管理系统', '登陆成功' if teacher_data else '账号或密码错误')
        if teacher_data:
            current_user = teacher_data
            
            self.pack_forget()
            add_frame.create()
            watching_frame.create()

    def register(self):
        
        teacher_data = session.query(Teacher).filter_by(name=self.teacher_name_var.get()).first()
        if teacher_data:
            messagebox.showinfo('学生信息管理系统', '账号已存在')
        else:
            teacher = Teacher()
            teacher.name = self.teacher_name_var.get()
            teacher.password = self.teacher_password_var.get()
            session.add(teacher)
            session.commit()
            messagebox.showinfo('学生信息管理系统', '注册成功,请返回登录')

    def create(self):
        self.create_window()
        self.pack(pady=130)


class AddFrame(tk.Frame):
    def __init__(self):
        super().__init__(master=root)
        self.name_variable = tk.StringVar()
        self.number_variable = tk.StringVar()
        self.chinese_variable = tk.StringVar()
        self.math_variable = tk.StringVar()
        self.english_variable = tk.StringVar()

    def create_window(self):
        tk.Label(self, text='姓名:').grid(row=0, column=0, pady=5)
        tk.Entry(self, textvariable=self.name_variable).grid(row=0, column=1, pady=5)
        tk.Label(self, text='座号:').grid(row=1, column=0, pady=5)
        tk.Entry(self, textvariable=self.number_variable).grid(row=1, column=1, pady=5)
        tk.Label(self, text='语文成绩:').grid(row=2, column=0, pady=5)
        tk.Entry(self, textvariable=self.chinese_variable).grid(row=2, column=1, pady=5)
        tk.Label(self, text='数学成绩:').grid(row=3, column=0, pady=5)
        tk.Entry(self, textvariable=self.math_variable).grid(row=3, column=1, pady=5)
        tk.Label(self, text='英语成绩:').grid(row=4, column=0, pady=5)
        tk.Entry(self, textvariable=self.english_variable).grid(row=4, column=1, pady=5)
        tk.Button(self, text='添加学生数据', command=self.add_student).grid(row=5, columnspan=2, pady=5)
        

    def add_student(self):
        student1 = Student()
        student1.name = self.name_variable.get()
        student1.chinese = self.chinese_variable.get()
        student1.math = self.math_variable.get()
        student1.english = self.english_variable.get()
        student1.number = self.number_variable.get()
        student1.teachers.append(current_user) if current_user not in student1.teachers else ''
        if not session.query(Student).filter_by(number=student1.number):
            session.add(student1)
            messagebox.showinfo('学生信息管理系统', '学生信息添加成功')
            session.commit()
            watching_frame.load_table()
        else:
            messagebox.showinfo('学生信息管理系统', '学生已存在')

    def create(self):
        self.create_window()
        self.pack()
        notebook.add(self, text='add')
        


class ChangeFrame(tk.Frame):
    def __init__(self, master, name, number, chinese, math, english):
        super().__init__(master=master)
        self.original_number = number
        self.master = master
        self.name_variable = tk.StringVar()
        self.number_variable = tk.StringVar()
        self.chinese_variable = tk.StringVar()
        self.math_variable = tk.StringVar()
        self.english_variable = tk.StringVar()
        self.name_variable.set(name)
        self.number_variable.set(number)
        self.chinese_variable.set(chinese)
        self.math_variable.set(math)
        self.english_variable.set(english)

    def create_window(self):
        tk.Label(self, text='姓名:').grid(row=0, column=0, pady=5)
        tk.Entry(self, textvariable=self.name_variable).grid(row=0, column=1, pady=5)
        tk.Label(self, text='座号:').grid(row=1, column=0, pady=5)
        tk.Entry(self, textvariable=self.number_variable).grid(row=1, column=1, pady=5)
        tk.Label(self, text='语文成绩:').grid(row=2, column=0, pady=5)
        tk.Entry(self, textvariable=self.chinese_variable).grid(row=2, column=1, pady=5)
        tk.Label(self, text='数学成绩:').grid(row=3, column=0, pady=5)
        tk.Entry(self, textvariable=self.math_variable).grid(row=3, column=1, pady=5)
        tk.Label(self, text='英语成绩:').grid(row=4, column=0, pady=5)
        tk.Entry(self, textvariable=self.english_variable).grid(row=4, column=1, pady=5)
        tk.Button(self, text='修改学生数据', command=self.change_student).grid(row=5, columnspan=2, pady=5)
        

    def change_student(self):
        student1 = session.query(Student).filter_by(number=self.original_number).first()
        student1.name = self.name_variable.get()
        student1.chinese = self.chinese_variable.get()
        student1.math = self.math_variable.get()
        student1.english = self.english_variable.get()
        student1.number = self.number_variable.get()
        student1.teachers.append(current_user) if current_user not in student1.teachers else ''
        session.commit()
        messagebox.showinfo('学生信息管理系统', '学生信息修改成功')
        self.master.destroy()
        watching_frame.load_table()
        


class WatchingFrame(tk.Frame):
    def __init__(self):
        super().__init__(master=root)
        self.table_columns = ['c1', 'c2', 'c3', 'c4', 'c5']

    def create_window(self):
        self.create_table()
        

    def create_table(self):
        table_frame = tk.Frame(self)
        xscroll = Scrollbar(table_frame, orient='horizontal')
        yscroll = Scrollbar(table_frame, orient='vertical')
        xscroll.pack(side='bottom', fill='x')
        yscroll.pack(side='right', fill='y')
        self.table = Treeview(master=table_frame,
                              columns=self.table_columns,
                              height=10,
                              show='headings',
                              xscrollcommand=xscroll.set,
                              yscrollcommand=yscroll.set)
        self.table.heading('c1', text='姓名')
        self.table.column('c1', width=60, anchor='center')
        self.table.heading('c2', text='座号')
        self.table.column('c2', width=60, anchor='center')
        self.table.heading('c3', text='语文')
        self.table.column('c3', width=60, anchor='center')
        self.table.heading('c4', text='数学')
        self.table.column('c4', width=60, anchor='center')
        self.table.heading('c5', text='英语')
        self.table.column('c5', width=60, anchor='center')
        xscroll.config(command=self.table.xview)
        yscroll.config(command=self.table.yview)
        self.table.pack(side='top')
        table_frame.pack(side='top')
        self.load_table()
        buttons_frame = tk.Frame(self)
        tk.Button(buttons_frame, text='刷新数据', command=self.load_table).grid(row=0, column=0)
        tk.Button(buttons_frame, text='删除此数据', command=self.delete_data).grid(row=0, column=1)
        tk.Button(buttons_frame, text='修改数据', command=self.change_data).grid(row=0, column=2)
        buttons_frame.pack(side='bottom')

    def get_focus(self):
        focus_id = self.table.focus()
        focus_data = self.table.item(focus_id)['values']
        if not focus_data:
            messagebox.showerror('学生信息管理系统', '请选中行！')
            return
        return focus_id, focus_data

    def delete_data(self):
        focus_id, focus_data = self.get_focus()
        yes_or_no = messagebox.askyesno('学生信息管理系统', '确定要删除吗?删除后将无法恢复!')
        if yes_or_no:
            data = dict(zip(['name', 'number', 'chinese', 'math', 'english'], focus_data))
            delete_data = session.query(Student).filter_by(**data).first()
            session.delete(delete_data)
            session.commit()
            messagebox.showinfo('学生信息管理系统', '删除成功！')
            self.table.delete(focus_id)
            

    def load_table(self):
        
        students = session.query(Teacher).get(current_user.id).students
        print(students)
        for child in self.table.get_children():
            self.table.delete(child)
        for student in students:
            self.table.insert('', 'end',
                              values=[student.name, student.number, student.chinese, student.math, student.english])

    def change_data(self):
        focus_data = self.get_focus()[1]
        top_level = tk.Toplevel(self)
        top_level.geometry('500x400')
        change_frame = ChangeFrame(top_level, focus_data[0], focus_data[1], focus_data[2], focus_data[3], focus_data[4])
        change_frame.create_window()
        change_frame.pack()

    def create(self):
        self.create_window()
        self.pack()
        notebook.add(self, text='watch')


login_frame = LoginFrame()
login_frame.create()
add_frame = AddFrame()
watching_frame = WatchingFrame()
notebook.pack()

root.mainloop()
